<?php
/**
 * 环信发送消息
 * zhouliang
 * 2020.09.10
 */

namespace App\Services\Tools;

class PushChatService
{
    
    const AREA_ALL = 0; //全部
    const AREA_ROOM = 1;//房间
    const AREA_LUCKY = 2;//幸运
    const AREA_WELCOME = 3;//欢迎
    const AREA_WORLD = 4;//世界
    
    const CHAT_GET_IN_ROOM         = 6001;//公屏消息-加入房间
    const CHAT_GIVE_GIFT           = 6002;//公屏消息-送礼物
    const CHAT_GIVE_GIFT_PRIVATE   = 6003;//私聊消息-送礼物
    const CHAT_FISH                = 6004;//公屏消息-小猫钓鱼礼物消息
    const CHAT_BALL                = 6005;//公屏消息-球球大作战
    const CHAT_PIT                 = 6006;//公屏消息-上下麦
    const CHAT_KICK_OUT            = 6007;//公屏消息-踢出房间
    const CHAT_SHUT_UP             = 6008;//公屏消息-禁麦|解禁
    const CHAT_BANNED              = 6009;//公屏消息-禁言|解禁
    const CHAT_FACE                = 6010;//公屏消息-发送表情
    const CHAT_ROLL                = 6011;//公屏消息-摇骰子
    const CHAT_CHAT_ROOM           = 6012;//公屏消息-普通文本消息
    const CHAT_OFFICIAL_NOTICE     = 6013;//公屏消息-官方公告
    const CHAT_GREETING            = 6014;//公屏消息-房间欢迎语
    const CHAT_CHAT_PRIVATE        = 6015;//私聊消息-普通文本消息
    const CHAT_NEW_USER            = 6016;//公屏消息-新用户加入平台
    const CHAT_BALL_START          = 6017;//公屏消息-开球
    const CHAT_BALL_THROW          = 6018;//公屏消息-丢球
    const CHAT_BALL_SHOW           = 6019;//公屏消息-亮球
    const CHAT_SOUND_EFFECT        = 6020;//公屏消息-音效改变
    const OPEN_BLIND_BOX           = 6021;//开盲盒送礼物公屏消息
    const NOTICE_BOX               = 6022;//开盲盒送礼物提示消息
    const ORDER_MESSAGE            = 6023;//订单消息
    const DEMAND_MESSAGE           = 6024;//发布派单后发送公屏消息
    const CP_APPLY                 = 6025;//人脉CP绑定申请私聊消息
    const CP_APPLY_AGREE           = 6026;//人脉CP绑定同意消息
    const CP_APPLY_REFUSE          = 6027;//人脉CP绑定拒绝消息
    const CP_APPLY_DIY_NICKNAME    = 6028;//人脉自定义称谓消息
    const MARRY_APPLY              = 6029;//人脉结婚申请消息
    const CONNECTION_INTIMACY_USED = 6030;//人脉亲密卡使用成功消息
    const CONNECTION_UNBIND        = 6031;//人脉解绑消息
    const CHAT_AUCTION_ABORT       = 6032;//拍卖中断发送公屏消息
    const CP_APPLY_EXPIRE          = 6033;//人脉CP绑定申请过期私聊消息
    const SYSTEM_NOTICE            = 6034;//公共的系统消息通知
    const CHAT_USER_FAVOR_ROOM     = 6035;//用户在房内关注房间
    const CHAT_THEME_HEAD_LINE     = 6036;//头条消息
    const CHAT_THEME_CHAT          = 6037;//话题广场消息
    const WELCOME_USER_JOIN_ROOM   = 6050;//欢迎他人进入房间（客户端用）

    public $user_id, $room_id, $topic_room, $topic_client;

    public function __construct($user_id = 0, $room_id = 0)
    {
        $this->user_id = $user_id;
        $this->room_id = $room_id;

        $this->topic_room = 'room_'.$this->room_id;
        $this->topic_client = 'user_'.$this->user_id;
    }

    public function setUser($user_id)
    {
        $this->user_id = $user_id;
        $this->topic_client = 'user_'.$user_id;
    }

    public function setRoom($room_id)
    {
        $this->room_id = $room_id;
        $this->topic_room = 'room_'.$room_id;
    }

    private function push($targetId, $message, $ext = [], $senderId = 'system', $type = 'RC:TxtMsg')
    {
        $this->rcQueue($targetId, $message, $ext, $senderId, $type);
    }

    public function rcQueue($targetId, $message, $ext = [], $senderId = 'system', $type = 'RC:TxtMsg')
    {
        /**
         * @var $rcService RongCloudService
         */
        $data = [
            'content' => $message,
            'extra' => $ext
        ];
        $rcService = app('rongcloud');
        $rcService->queue($senderId, $targetId, $data, 0, $type);
    }

    //用户进入房间
    public function getInRoom($ext = [])
    {
        $ext['type'] = self::CHAT_GET_IN_ROOM;
        $ext['action'] = 3;
        $message = '<font color="#FFCB00">加入直播间</font>';
        $this->push($this->topic_room, $message, $ext);
    }

    //用户在房间赠送礼物
    public function giveGift($get_users, $ext = [])
    {
        $ext['type'] = self::CHAT_GIVE_GIFT;
        $ext['action'] = 3;
        $message = '<font color="#FF6C6C">送了</font><font color="#B6A2FF">'.implode('，',
                $get_users).'</font><font color="#FF6C6C">'.$ext['gift_name'].'x'.$ext['gift_num'].'</font>';
        $this->push($this->topic_room, $message, $ext);
//          Log::record('修改后的推送：'.$message);
    }


    //用户开启盲盒提示消息
    public function noticeBox($target, $gift_given_user_ids = [], $ext = [])
    {
        $ext['type'] = self::NOTICE_BOX;
        $ext['action'] = 3;
        $message = '<font color="#FF6C6C">送给</font><font color="#B6A2FF">'.implode('，', $gift_given_user_ids).'</font>';
        $this->push($target, $message, $ext);
    }

    //上下麦
    public function pit($message, $ext)
    {
        $ext['type'] = self::CHAT_PIT;
        $ext['action'] = 3;
        $message = '<font color="#9866FF">'.$message.'</font>';
        $this->push($this->topic_room, $message, $ext);
    }

    //踢出房间
    public function kickOut($ext)
    {
        $ext['type'] = self::CHAT_KICK_OUT;
        $ext['action'] = 3;
        $message = '<font color="#FFFFFF">被踢出房间</font>';
        $this->push($this->topic_room, $message, $ext);
    }

    //公屏消息-普通文本消息
    public function chatRoom($message, $ext)
    {
        $ext['type'] = self::CHAT_CHAT_ROOM;
        $ext['action'] = 2;
        $this->push($this->topic_room, $message, $ext);
    }

    //下订单环信消息
    public function orderProduce($targetId, $param = [], $senderId)
    {
        $ext['type'] = self::ORDER_MESSAGE;
        $ext['em_is_order'] = true;
        $ext['order_status'] = $param['status'];
        $ext['order_title'] = $param['title'];
        $ext['order_desc'] = $param['desc'];
        $ext['order_type'] = $param['type'];
        $ext['order_id'] = $param['order_id'];
        $ext['user_id'] = $param['user_id'];
        $ext['nickname'] = $param['nickname'];
        $ext['sex'] = $param['sex'];
        $ext['avatar'] = $param['avatar'];
        $this->push('user_' . $targetId, '[订单消息]', $ext, 'user_' . $senderId);
    }

    //发布派单后发送公屏消息
    public function demandMessage($message)
    {
        $ext['type'] = self::DEMAND_MESSAGE;
        $ext['action'] = 9;
        $ext['room_id'] = $this->room_id;
        $this->push($this->topic_room, $message, $ext);
    }

    //人脉申请CP绑定请求发送申请消息
    public function cpApply($targetId, $ext, $senderId)
    {
        $ext['type'] = self::CP_APPLY;
        $message = '用户'.$ext['nickname'] ?? ''.'（'.$ext['user_code'] ?? ''.'）'.'申请与你绑定称谓CP';
        $this->push('user_' . $targetId, $message, $ext, 'user_' . $senderId);
    }

    //人脉申请CP绑定请求过期消息
    public function cpApplyExpire($targetId, $ext = [], $senderId)
    {
        $ext['type'] = self::CP_APPLY_EXPIRE;
        $this->push('user_' . $targetId, '绑定申请已失效', $ext, 'user_' . $senderId);
    }

    //人脉申请CP绑定请求同意消息
    public function cpApplyAgree($targetId, $type, $ext = [], $senderId)
    {
        $ext['type'] = self::CP_APPLY_AGREE;
        $message = '恭喜二位喜结'.($type == 1 ? 'cp' : '良缘');
        $this->push('user_' . $targetId, $message, $ext, 'user_' . $senderId);
    }

    //人脉申请CP绑定请求拒绝消息
    public function cpApplyRefuse($targetId, $ext = [], $senderId)
    {
        $ext['type'] = self::CP_APPLY_REFUSE;
        $this->push('user_' . $targetId, '感谢您的喜欢，我们不合适！', $ext, 'user_' . $senderId);
    }

    //人脉自定义称谓消息
    public function cpApplyDiyNickname($targetId, $ext = [], $senderId)
    {
        $ext['type'] = self::CP_APPLY_DIY_NICKNAME;
        $this->push('user_' . $targetId, '嗨，以后我就叫你 '.$ext['user_nickname'], $ext, 'user_' . $senderId);
    }

    //人脉申请结婚请求发送申请消息
    public function marryApply($targetId, $ext, $senderId)
    {
        $ext['type'] = self::MARRY_APPLY;
        $message = '用户'.$ext['nickname'] ?? ''.'（'.$ext['user_code'] ?? ''.'）'.'申请与你结婚';
        $this->push('user_' . $targetId, $message, $ext, 'user_' . $senderId);
    }

    //人脉亲密卡使用成功消息
    public function intimacyUsed($targetId, $ext = [], $senderId)
    {
        $ext['type'] = self::CONNECTION_INTIMACY_USED;
        $this->push('user_' . $targetId, '我使用了亲密卡，我们继续提升亲密等阶吧！', $ext, 'user_' . $senderId);
    }

    //人脉解绑消息
    public function unbind($targetId, $ext = [], $senderId)
    {
        $title = '';
        if ($ext['type'] == 2 && $ext['married']) {
            $title = '婚姻';
        } elseif ($ext['type'] == 2 && !$ext['married']) {
            $title = 'CP';
        } else {
            $title = '亲密';
        }
        $message = '您们的'.$title.'关系已解除';
        $ext['type'] = self::CONNECTION_UNBIND;
        $this->push('user_' . $targetId, $message, $ext, 'user_' . $senderId);
    }

    //拍卖中断
    public function auctionAbort($message)
    {
        $ext['type'] = self::CHAT_AUCTION_ABORT;

        $this->push($this->topic_room, $message, $ext);
    }

    //公共的系统消息通知
    public function systemNotice($message)
    {
        $ext['type'] = self::SYSTEM_NOTICE;

        $this->push($this->topic_room, $message, $ext);
    }
    
    //发头条公屏通知 （全服通知）
    public function themeStriveHeadlineChat($message, $ext){
        $ext['type'] = self::CHAT_THEME_HEAD_LINE;
        $ext['action'] = 3;
        $ext['show_area'] = [
            self::AREA_WORLD,
        ];
        $message = '<font color="#ECE9FF">'.$message.'</font>';
        //全服推送
        $this->push('room', $message, $ext);
    }
    //话题广场推送消息
    public function themeStriveChat($message, $ext){
        $ext['type'] = self::CHAT_THEME_CHAT;
        $ext['action'] = 3;
        $ext['show_area'] = [
            self::AREA_WORLD,
        ];
        $message = '<font color="#FFFFFF">'.$message.'</font>';
        //全服推送
        $this->push('room', $message, $ext);
    }
}

